/**
 * Filename: AboutActivity.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/9 2:57
 */
package com.github.postsanf.yinian.activity;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.BaseApplication;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.constants.ISkinUpdate;
import com.github.postsanf.yinian.service.UpdateManager;
import com.github.postsanf.yinian.utils.DisplayUtils;
import com.github.postsanf.yinian.utils.SkinPackageManager;
import com.litesuits.http.exception.HttpException;
import com.litesuits.http.listener.HttpListener;
import com.litesuits.http.request.StringRequest;
import com.litesuits.http.request.param.HttpMethods;
import com.litesuits.http.response.Response;
import com.umeng.analytics.MobclickAgent;

import org.json.JSONObject;

import java.util.LinkedHashMap;

/**
 * 关于忆年
 */
public class AboutActivity extends BaseActivity implements View.OnClickListener, ISkinUpdate {
    private Toolbar mToolbar;
    private TextView app_version;                    //版本
    private RelativeLayout rl_about_checkupdate;           //检测更新
    private RelativeLayout rl_about_service;               //  用户协议
    private RelativeLayout rl_about_support;               //  支持忆年
    private RelativeLayout rl_about_yinian;
    private RelativeLayout rl_about_introduct;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yn_activity_about);
        BaseApplication.getInstance().mActivitys.add(this);
        initView();
    }

    private void initView() {
        mToolbar = (Toolbar) findViewById(R.id.about_toolbar);
        setSupportActionBar(mToolbar);
        mToolbar.setNavigationIcon(R.drawable.ic_return);
        app_version = (TextView) findViewById(R.id.app_version);
        rl_about_checkupdate = (RelativeLayout) findViewById(R.id.rl_about_checkupdate);
        rl_about_service = (RelativeLayout) findViewById(R.id.rl_about_service);
        rl_about_introduct = (RelativeLayout) findViewById(R.id.rl_about_introduct);
        rl_about_support = (RelativeLayout) findViewById(R.id.rl_about_support);
        rl_about_yinian = (RelativeLayout) findViewById(R.id.rl_about_yinian);
        app_version.setText(DisplayUtils.getAPPVersionCodeFromAPP(this));
        rl_about_checkupdate.setOnClickListener(this);
        rl_about_service.setOnClickListener(this);
        rl_about_introduct.setOnClickListener(this);
        rl_about_support.setOnClickListener(this);
        rl_about_yinian.setOnClickListener(this);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void checkUpdataAPP() {
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        StringRequest req = new StringRequest(getString(R.string.CHECK_VERSION_UPDATE))
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        try {
                            JSONObject updatejson = new JSONObject(s.toString());
                            if (updatejson.optInt("code") == 0) {
                                String versionNo = updatejson.optJSONArray("data").optJSONObject(0).optString("versionNo");
                                String versionUrl = updatejson.optJSONArray("data").optJSONObject(0).optString("versionUrl");
                                UpdateManager updateManager = new UpdateManager(versionNo, versionUrl, "检测到有新的版本，请下载更新！", AboutActivity.this);
                                updateManager.checkUpdate2();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
                        Toast.makeText(AboutActivity.this, CommonConstants.YN_NET_LINK, Toast.LENGTH_SHORT).show();
                    }
                });
        liteHttp.executeAsync(req);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_about_checkupdate:
                checkUpdataAPP();
                break;
            case R.id.rl_about_service:
                intent2Activity(AgreementActivity.class);
                break;
            case R.id.rl_about_support:
                try {
                    Uri uri = Uri.parse("market://details?id=" + getPackageName());
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                } catch (ActivityNotFoundException e) {
                    showToast("未找到应用市场");
                }
                break;
            case R.id.rl_about_yinian:
                intent2Activity(WhatsYNActivity.class);
                break;
        }
    }

    @Override
    public void updateTheme() {
        if (mToolbar != null) {
            try {
                Resources mResource = SkinPackageManager.getInstance(this).mResources;
                mToolbar.setNavigationIcon(mResource.getDrawable(mResource.getIdentifier("ic_return", "drawable", "com.github.postsanf.skinby")));
                mToolbar.setBackgroundColor(mResource.getColor(mResource.getIdentifier("yncolor", "color", "com.github.postsanf.skinby")));
                mToolbar.setTitleTextColor(mResource.getColor(mResource.getIdentifier("border_color", "color", "com.github.postsanf.skinby")));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        BaseApplication.getInstance().mActivitys.remove(this);
    }

    // 友盟统计
    public void onResume() {
        super.onResume();
        if (SkinPackageManager.getInstance(this).mResources != null) {
            updateTheme();
        }
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }
}
